// pages/manageGoods/manageGoods.js

var app = getApp();
var page=1;
var selectedNews = false;
var newsList = [];

Page({
  data:{
    selected: true,
    goodsList: [],
    unreadnum: 0,
    unreadnum2: 0,
    btuBottom: 0
  },
  //点击进入商品编辑页面
  bindGoodsTap: function (e) {
    var id = e.detail.button_typId;
    wx.navigateTo({
      url: '../goodsEdit/goodsEdit?id=' + id,
    })
  },

  // 打开雷达互动页面
  goRadarCenter: function () {
    const that = this
    wx.showLoading({
      mask: 'true'
    })
    wx.redirectTo({
      url: '/radarPages/radarCenter/radarCenter',
      success() {
        wx.hideLoading()
      },
      fail() {
        wx.hideLoading()
      }
    })
  },

  // 打开聊天列表页面
  goContactList: function () {
    const that = this
    wx.redirectTo({
      url: '/pages/contactList/contactList'
    })
  },

  // 打开个人名片页面
  goMyCarte: function () {
    const that = this
    wx.redirectTo({
      url: '/pages/myCarte/myCarte'
    })
  },

  // 返回名片首页
  goPerson: function () {
    const that = this
    wx.switchTab({
      url: '/pages/person/person',
    })
  },

  //点添加图标进入商品新增页面
  bindAddGoods: function (e) {
    wx.navigateTo({
      url: '../goodsEdit/goodsEdit?uid=' + app.globalData.userInfo.id
    })
  },

  //获取商品列表
  getGoodsList: function () {
    var that = this;
    var url2 = '/shop_api/goodsList';
    var params2 = {
      mgId: app.globalData.userInfo.id,
      pagesize: 20,
      page: page
    };
    app.HttpPostSend(url2, params2, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        selectedNews = false;
        that.setData({
          goodsList: []
        })
      } else {
        if (res.data.length>0){
          var goodsList = res.data;
          var img_show = false;
          for (let i = 0; i < goodsList.length; i++) {
            // goodsList[i].img_show = img_show;
            newsList.push(goodsList[i]);
          }
          if (goodsList.length < 20) {
            selectedNews = false;
          } else {
            selectedNews = true;
            page += 1;
          }
          that.setData({
            goodsList: newsList
          })
          // let goodsList = that.data.goodsList  // 获取原数据
          // for (let i in goodsList) {
          //   wx.createIntersectionObserver().relativeToViewport({ bottom: 0 }).observe('.item-' + i, (ret) => {
          //     if (ret.intersectionRatio > 0) {
          //       goodsList[i].img_show = true
          //     }
          //     that.setData({ // 更新数据
          //       goodsList: goodsList
          //     })
          //   })
          // }
          wx.hideLoading();
        }
      }
    });
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    var that = this;
    wx.getSystemInfo({
      success: res => {
        // console.log('手机信息res'+res.model)
        let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1) {
          that.setData({
            btuBottom: '42rpx'
          });
        }
      }
    })
    var url2 = '/log_api/logReadNum';
    var params2 = {
      uid: app.globalData.userInfo.id
    };
    app.HttpGetSend(url2, params2, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        var count = res.data;
        if (count > 99){
          count = 99;
        }
        that.setData({
          unreadnum: count
        })
      }
    });

    var url3 = '/chat_api/getReadNum';
    var params3 = {
      uid: app.globalData.userInfo.id
    };
    app.HttpGetSend(url3, params3, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        var count = res.data;
        if (count > 99) {
          count = 99;
        }
        that.setData({
          unreadnum2: count
        })
      }
    });
    page = 1;
    selectedNews = false;
    newsList = [];
    that.getGoodsList();
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  // onShareAppMessage: function () {
  //   return {
  //     title: '万商',
  //     desc: '你要的名片',
  //     path: '/pages/home/home'
  //   }
  // },
  /** 
   * 页面上拉触底事件的处理函数 
   */
  onReachBottom: function () {
    if (selectedNews == true){
      var that = this;
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1  
      that.getGoodsList();

    }else{
      wx.hideLoading();
    }
  }
})